package com.yaodian100.crm.activities.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.transaction.annotation.Transactional;

import com.yaodian100.core.common.service.BaseManager;
import com.yaodian100.core.common.service.impl.DomainServiceImpl;
import com.yaodian100.crm.activities.entity.PromotionOuter;
import com.yaodian100.ec.product.entity.Product;
import com.yaodian100.ec.product.entity.Specification;

public class PromotionOuterServiceImp extends DomainServiceImpl<PromotionOuter> implements PromotionOuterService {
	@Resource
	private BaseManager baseMgr;

	@Override
	@Transactional(readOnly = false)
	public String savemain(PromotionOuter promotionOuter) {
		baseMgr.save(promotionOuter);
		return null;
	}

	@Override
	public List<PromotionOuter> findByJDGiftMain(Date endDate) {

		List<PromotionOuter> list = new ArrayList<PromotionOuter>();

		Object[] type = baseMgr.queryByHql("from PromotionOuter where endDate<=? ", new Object[] { endDate });
		if (type.length > 0) {

			for (Object object : type) {

				PromotionOuter couponItem = (PromotionOuter) object;

				list.add(couponItem);

			}
		}
		return list;

	}

	@Override
	public String fingProductId(int id) {

		Specification specification = new Specification();

		Object[] types = baseMgr.queryByHql("from Specification where specificationId =? ", new Object[] { id });
		if (types.length > 0) {

			for (Object object : types) {

				specification = (Specification) object;
			}
		}
		if (specification != null) {

			return specification.getProductId();

		}

		return null;
	}

	@Override
	public String fingProductName(String id) {

		Specification specification = new Specification();

		Object[] types = baseMgr.queryByHql("from Specification where specificationId =? ", new Object[] { id });
		if (types.length > 0) {

			for (Object object : types) {

				specification = (Specification) object;
			}
		}
		if (specification != null) {

			String productId = specification.getProductId();
			Product product = new Product();

			Object[] typep = baseMgr.queryByHql("from Product where productId =? ", new Object[] { productId });
			if (typep.length > 0) {

				for (Object object : typep) {

					product = (Product) object;

				}
			}
			return product.getProductName();
		}

		return "没找到商品";
	}
}
